package cc.telecomdigital.tdstock.Http;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import cc.telecomdigital.tdstock.CustomizeDialog;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Http.Services.H_HttpServiceRequest.H_UserClient_HttpRequest_Result;
import cc.telecomdigital.tdstock.Http.Services.H_HttpServiceRequest.IH_UserClient_HttpRequest_Report;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.BaseActivity;

/**
 * Super class Send http request activity
 * 
 * @author ouyangzhihui
 */
public abstract class HttpSuperClassStockBaseActivity extends BaseActivity implements IH_UserClient_HttpRequest_Report
{
	static final private String	LOG_TAG						= "HttpSuperClassActivity-StockBase";

	private boolean				isExectuHttpReqestOnResume	= true;
	private boolean				iHConnectIsDestory			= false;

	protected ProgressView		progressView				= null;

	@Override
	protected ActivityGroupController GetActivityGroupController()
	{
		return null;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		progressView = new ProgressView(GetShowDialogContext());
	}

	protected void SetExectuHttpRequestOnResume(boolean iExecute)
	{
		isExectuHttpReqestOnResume = iExecute;
	}

	@Override
	protected void onResume()
	{
		TDStockLog.d(LOG_TAG, "onResume: screen lock " + appHost.screenIsLocked);

		iHConnectIsDestory = false;

		super.onResume();

		if (isExectuHttpReqestOnResume)
			SendHttpRequest();
	}

	@Override
	protected void onPause()
	{
		progressView.DismissProgressAll();

		iHConnectIsDestory = true;
		super.onPause();
	}

	@Override
	protected void onDestroy()
	{
		progressView.DismissProgressAllOnDestory();

		iHConnectIsDestory = true;
		super.onDestroy();
	}
	
	protected Context GetShowDialogContext()
	{
		return this.getParent() == null ? this : this.getParent();
	}
	
	protected void ShowMessageDialog(String msgContent)
	{
		CustomizeDialog promptBox;
		promptBox = new CustomizeDialog(GetShowDialogContext(), "提示", msgContent)
		{
			@Override
			public void onClick(View v)
			{
				dismissAll();
			}
		};
		promptBox.setCancelable(true);
		promptBox.show();
	}

	protected void SendHttpRequest()
	{
		if (appHost.screenIsLocked)
			return;

		TDStockLog.d(LOG_TAG, "SendHttpRequest=>screen lock " + appHost.screenIsLocked);

		ShowHideDialog(GetDataFromHttpServer());
	}

	protected abstract H_CODE GetDataFromHttpServer();

	protected void ShowHideDialog(H_CODE statusCode)
	{
		if (statusCode == null)
			return;

		if (statusCode == H_CODE.NONE)
			return;

		if (statusCode == H_CODE.OK || statusCode == H_CODE.SYSTEM_BUSY)
			progressView.ShowProgress();
		else ShowMessageDialog(statusCode.ToString());
	}

	@Override
	public void H_UserClient_HttpRequest_Response(H_UserClient_HttpRequest_Result iResult)
	{
		progressView.DismissProgress();

		if (iHConnectIsDestory)
			return;

		H_UserClient_HttpRequest_Response_Sub(iResult);
	}

	protected abstract void H_UserClient_HttpRequest_Response_Sub(H_UserClient_HttpRequest_Result iResult);
}
